home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.3 / mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-01  |  4.9 KB  |  200 lines

  1. #include "mesh.h"
  2.  
  3. const DWORD ObjectVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
  4.  
  5. MESH::MESH()
  6. {
  7.     m_pDevice = NULL;
  8.     m_pMesh = NULL;
  9. }
  10.  
  11. MESH::MESH(char fName[], IDirect3DDevice9* Dev)
  12. {
  13.     m_pDevice = Dev;
  14.     m_pMesh = NULL;
  15.     Load(fName, m_pDevice);
  16. }
  17.  
  18. MESH::~MESH()
  19. {
  20.     Release();
  21. }
  22.  
  23. HRESULT MESH::Load(char fName[], IDirect3DDevice9* Dev)
  24. {
  25.     m_pDevice = Dev;
  26.  
  27.     //Set white material
  28.     m_white.Ambient = m_white.Specular = m_white.Diffuse  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  29.     m_white.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  30.     m_white.Power = 1.0f;
  31.  
  32.     Release();
  33.  
  34.     //Load new mesh
  35.     ID3DXBuffer * adjacencyBfr = NULL;
  36.     ID3DXBuffer * materialBfr = NULL;
  37.     DWORD noMaterials = NULL;
  38.  
  39.     if(FAILED(D3DXLoadMeshFromX(fName, D3DXMESH_MANAGED, m_pDevice,
  40.                                 &adjacencyBfr, &materialBfr, NULL, 
  41.                                 &noMaterials, &m_pMesh)))
  42.         return E_FAIL;
  43.  
  44.     D3DXMATERIAL *mtrls = (D3DXMATERIAL*)materialBfr->GetBufferPointer();
  45.  
  46.     for(int i=0;i<noMaterials;i++)
  47.     {
  48.         m_materials.push_back(mtrls[i].MatD3D);
  49.  
  50.         if(mtrls[i].pTextureFilename != NULL)
  51.         {
  52.             char textureFileName[90];
  53.             strcpy(textureFileName, "objects/");
  54.             strcat(textureFileName, mtrls[i].pTextureFilename);
  55.             IDirect3DTexture9 * newTexture = NULL;
  56.             D3DXCreateTextureFromFile(m_pDevice, textureFileName, &newTexture);            
  57.             m_textures.push_back(newTexture);
  58.         }
  59.         else m_textures.push_back(NULL);
  60.     }
  61.  
  62.     m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
  63.                             (DWORD*)adjacencyBfr->GetBufferPointer(), NULL, NULL, NULL);
  64.  
  65.     adjacencyBfr->Release();
  66.     materialBfr->Release();
  67.  
  68.     return S_OK;
  69. }
  70.  
  71. void MESH::Render()
  72. {
  73.     for(int i=0;i<m_materials.size();i++)
  74.     {    
  75.         if(m_textures[i] != NULL)m_pDevice->SetMaterial(&m_white);
  76.         else m_pDevice->SetMaterial(&m_materials[i]);
  77.         m_pDevice->SetTexture(0,m_textures[i]);
  78.         m_pMesh->DrawSubset(i);
  79.     }    
  80. }
  81.  
  82. void MESH::Release()
  83. {
  84.     //Clear old mesh...
  85.     if(m_pMesh != NULL)
  86.     {
  87.         m_pMesh->Release();
  88.         m_pMesh = NULL;
  89.     }
  90.  
  91.     //Clear textures and materials
  92.     for(int i=0;i<m_textures.size();i++)
  93.         if(m_textures[i] != NULL)
  94.             m_textures[i]->Release();
  95.  
  96.     m_textures.clear();
  97.     m_materials.clear();    
  98. }
  99.  
  100. MESHINSTANCE::MESHINSTANCE()
  101. {
  102.     m_pMesh = NULL;
  103.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  104.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  105. }
  106.  
  107. MESHINSTANCE::MESHINSTANCE(MESH *meshPtr)
  108. {
  109.     m_pMesh = meshPtr;
  110.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  111.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  112. }
  113.  
  114. D3DXMATRIX MESHINSTANCE::GetWorldMatrix()
  115. {
  116.     D3DXMATRIX p, r, s;
  117.     D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z);
  118.     D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  119.     D3DXMatrixScaling(&s, m_sca.x, m_sca.y, m_sca.z);
  120.  
  121.     D3DXMATRIX world = s * r * p;
  122.     return world;
  123. }
  124.  
  125. void MESHINSTANCE::Render()
  126. {
  127.     if(m_pMesh != NULL)
  128.     {
  129.         m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &GetWorldMatrix());
  130.         m_pMesh->Render();
  131.     }
  132. }
  133.  
  134. BBOX MESHINSTANCE::GetBoundingBox()
  135. {
  136.     if(m_pMesh == NULL || m_pMesh->m_pMesh == NULL)return BBOX();
  137.     
  138.     if(m_pMesh->m_pMesh->GetFVF() != ObjectVertex::FVF)        // XYZ and NORMAL and UV
  139.         return BBOX();
  140.  
  141.     BBOX bBox(D3DXVECTOR3(-10000.0f, -10000.0f, -10000.0f),
  142.               D3DXVECTOR3(10000.0f, 10000.0f, 10000.0f));
  143.     D3DXMATRIX World = GetWorldMatrix();
  144.  
  145.     //Lock vertex buffer of the object
  146.     ObjectVertex* vertexBuffer = NULL;
  147.     m_pMesh->m_pMesh->LockVertexBuffer(0,(void**)&vertexBuffer);
  148.  
  149.     //For each vertex in the mesh
  150.     for(int i=0;i<m_pMesh->m_pMesh->GetNumVertices();i++)
  151.     {
  152.         //Transform vertex to world space using the MESHINSTANCE
  153.         //world matrix, i.e. the position, rotation and scale
  154.         D3DXVECTOR3 pos;
  155.         D3DXVec3TransformCoord(&pos, &vertexBuffer[i]._pos, &World);
  156.  
  157.         // Check if the vertex is outside the bounds
  158.         // if so, then update the bounding volume
  159.         if(pos.x < bBox.min.x)bBox.min.x = pos.x;
  160.         if(pos.x > bBox.max.x)bBox.max.x = pos.x;
  161.         if(pos.y < bBox.min.y)bBox.min.y = pos.y;
  162.         if(pos.y > bBox.max.y)bBox.max.y = pos.y;
  163.         if(pos.z < bBox.min.z)bBox.min.z = pos.z;
  164.         if(pos.z > bBox.max.z)bBox.max.z = pos.z;
  165.     }
  166.  
  167.     m_pMesh->m_pMesh->UnlockVertexBuffer();
  168.  
  169.     return bBox;
  170. }
  171.  
  172. BSPHERE MESHINSTANCE::GetBoundingSphere()
  173. {
  174.     if(m_pMesh == NULL || m_pMesh->m_pMesh == NULL)return BSPHERE();
  175.     if(m_pMesh->m_pMesh->GetFVF() != ObjectVertex::FVF)        // XYZ and NORMAL and UV
  176.         return BSPHERE();
  177.  
  178.     BBOX bBox = GetBoundingBox();
  179.     BSPHERE bSphere;
  180.     D3DXMATRIX World = GetWorldMatrix();
  181.     bSphere.center = (bBox.max + bBox.min) / 2.0f;
  182.  
  183.     ObjectVertex* vertexBuffer = NULL;
  184.     m_pMesh->m_pMesh->LockVertexBuffer(0,(void**)&vertexBuffer);
  185.  
  186.     //Get radius
  187.     for(int i=0;i<m_pMesh->m_pMesh->GetNumVertices();i++)
  188.     {
  189.         D3DXVECTOR3 pos;
  190.         D3DXVec3TransformCoord(&pos, &vertexBuffer[i]._pos, &World);
  191.  
  192.         float l = D3DXVec3Length(&(pos - bSphere.center));
  193.         if(l > bSphere.radius)
  194.             bSphere.radius = l;
  195.     }
  196.  
  197.     m_pMesh->m_pMesh->UnlockVertexBuffer();
  198.  
  199.     return bSphere;
  200. }